/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.types;

import java.util.Date;
import java.util.List;

import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.types.Reference;

import org.emergent.antbite.savant.Artifact;
import org.emergent.antbite.savant.ArtifactGroup;


/**
 * <p>
 * This class is an ant data type to stores a list of
 * artifacts that compose a logic group.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class ArtifactGroupType extends DataType {

    private ArtifactGroup proxy = new ArtifactGroup();


    /**
     * Constructs a new <code>ArtifactGroupType</code>.
     */
    public ArtifactGroupType() {
    }


    /**
     * Returns the ArtifactGroup that this class is a proxy to.
     */
    public ArtifactGroup getProxy() {
        return proxy;
    }

    /**
     * Copy everything from the reference into this object rather than delegate.
     */
    public void setRefid(Reference reference) {
        super.setRefid(reference);

        ArtifactGroupType group = (ArtifactGroupType) reference.getReferencedObject(getProject());
        List artifacts = group.getArtifacts();
        for (int i = 0; i < artifacts.size(); i++) {
            proxy.addArtifact((Artifact) artifacts.get(i));
        }

        proxy.setClasspathid(group.getClasspathid());
        proxy.setFilesetid(group.getFilesetid());
        proxy.setExpireminutes(group.getExpireminutes());
        proxy.setExpiretime(group.getExpiretime());
    }

    /**
     * Returns the live list of artifacts in this group.
     */
    public List getArtifacts() {
        return proxy.getArtifacts();
    }

    /**
     * Adds a new artifact to this group.
     *
     * @param   artifact The artifact
     */
    public void addConfiguredArtifact(ArtifactType artifact) {
        if (isReference()) {
            throw noChildrenAllowed();
        }

        if (artifact.getExpireminutes() == 0) {
            artifact.setExpireminutes(proxy.getExpireminutes());
        }

        if (artifact.getExpiretime() == null) {
            artifact.setExpiretime(proxy.getExpiretime());
        }

        proxy.addArtifact(artifact.getProxy());
    }

    /**
     * Adds a new nested artifact group or a reference. This group is completely
     * configured and therefore we just dump the artifacts from that group into
     * this group.
     *
     * @param   group The group to add
     */
    public void addConfiguredArtifactgroup(ArtifactGroupType group) {
        if (isReference()) {
            throw noChildrenAllowed();
        }

        List artifacts = group.getArtifacts();
        for (int i = 0; i < artifacts.size(); i++) {
            proxy.addArtifact((Artifact) artifacts.get(i));
        }
    }

    public String getClasspathid() {
        return proxy.getClasspathid();
    }

    public void setClasspathid(String classPathId) {
        if (isReference()) {
            throw tooManyAttributes();
        }

        proxy.setClasspathid(classPathId);
    }

    public String getFilesetid() {
        return proxy.getFilesetid();
    }

    public void setFilesetid(String fileSetId) {
        if (isReference()) {
            throw tooManyAttributes();
        }

        proxy.setFilesetid(fileSetId);
    }

    public int getExpireminutes() {
        return proxy.getExpireminutes();
    }

    public void setExpireminutes(int expireMinutes) {
        proxy.setExpireminutes(expireMinutes);
    }

    public Date getExpiretime() {
        return proxy.getExpiretime();
    }

    public void setExpiretime(Date expireTime) {
        proxy.setExpiretime(expireTime);
    }
}